# run_coroutine_threadsafe
import asyncio
from threading import Thread


def start_thread_loop(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()


async def thread_example(name):
    print('正在执行：', name)
    await asyncio.sleep(1)
    return '返回值：' + name


new_loop = asyncio.new_event_loop()
t = Thread(target=start_thread_loop, args=(new_loop,))
t.start()

future = asyncio.run_coroutine_threadsafe(thread_example('task_1'), new_loop)
print(future.result())

asyncio.run_coroutine_threadsafe(thread_example('task_2'), new_loop)
print('主线程不会阻塞')
asyncio.run_coroutine_threadsafe(thread_example('task_3'), new_loop)
asyncio.run_coroutine_threadsafe(thread_example('task_3'), new_loop)
print('继续运行中.....')
